home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / dirxcpp / rmbegin1.cpp < prev    next >
C/C++ Source or Header  |  1998-11-22  |  15KB  |  438 lines

  1. //-----------------------------------------------------------------------------
  2. // File: RMBegin1.cpp
  3. //
  4. // Desc: This minimal file creates a default device, loads a mesh from a 
  5. //       DirectX file, builds a scene, and rotates the mesh object. It also
  6. //       contains standard code which allows the sample to run in the
  7. //       Windows environment. 
  8. //
  9. // Copyright (C) 1998 Microsoft Corporation. All Rights Reserved.
  10. //
  11. // Modified by Weston Software to demonstrate DirectXCPP
  12. //-----------------------------------------------------------------------------
  13.  
  14. #define INITGUID       // Required for Direct3D applications 
  15. #include <windows.h>   // Standard Windows header file 
  16. #include <direct.h>    // DirectDraw definitions 
  17. #include "resource.h"
  18.  
  19. #include "dxcpp.h"     // DirectXCPP classes by Weston Software
  20. #include "spider.h"    // Animated spider class
  21.  
  22.  
  23. // Macro to display a message box containing the given string. 
  24. #define DISPLAYMSG(x) MessageBox(NULL, x, "D3DRM Sample", MB_OK);
  25.  
  26.  
  27. // Global Variables 
  28. CADirect3DRM *lpD3DRM = NULL;            // Direct3DRM object 
  29. CADirectDrawClipper *lpDDClipper = NULL; // DirectDrawClipper object 
  30.  
  31. // Global Structure
  32. struct _myglobs
  33. {
  34.     // Direct3DRM device. 
  35.     CADirect3DRMDevice * dev;           
  36.  
  37.     // Direct3DRM viewport through which to view the scene. 
  38.     CADirect3DRMViewport * view;  
  39.  
  40.     // Master frame in which other frames are placed. 
  41.     CADirect3DRMFrame * scene;    
  42.  
  43.     // Frame describing the users point of view. 
  44.     CADirect3DRMFrame * camera;   
  45.  
  46.     // mesh spider class
  47.     CASpider  *spider;
  48.  
  49.     // Application is minimized. 
  50.     BOOL bMinimized;            
  51.  
  52.     // All D3DRM objects have been initialized. 
  53.     BOOL bInitialized;          
  54.  
  55. } myglobs;
  56.  
  57.  
  58. // Prototypes 
  59. BOOL             BuildScene( CADirect3DRM *, CADirect3DRMDevice *, 
  60.                              CADirect3DRMFrame *, CADirect3DRMFrame * );
  61. static BOOL      RenderScene();
  62. BOOL             CreateObjects( HWND win );
  63.  
  64. static HWND      InitApp( HINSTANCE, int );
  65. int APIENTRY     WinMain( HINSTANCE, HINSTANCE, LPSTR, int );
  66. LRESULT CALLBACK WindowProc( HWND, UINT, WPARAM, LPARAM );
  67.  
  68.  
  69.  
  70.  
  71. //-----------------------------------------------------------------------------
  72. // Name: BuildScene()
  73. // Desc: Create the scene to be rendered. 
  74. //-----------------------------------------------------------------------------
  75. BOOL BuildScene( CADirect3DRM * lpD3DRM,  CADirect3DRMDevice * dev, 
  76.                  CADirect3DRMFrame * scene, CADirect3DRMFrame * camera )
  77. {
  78.     CADirect3DRMFrame *lights = NULL;
  79.     CADirect3DRMLight  *light1 = NULL;
  80.     CADirect3DRMLight  *light2 = NULL;
  81.  
  82.     CADirect3DRMFrame rotateframe( lpD3DRM, scene->I() );
  83.     rotateframe.SetRotation(scene->I(), D3DVAL(0), D3DVAL(1), D3DVAL(0), D3DVAL(-0.01)); // angle 
  84.  
  85.     myglobs.spider = new CASpider( lpD3DRM, rotateframe.I() );
  86.     myglobs.spider->SetPosition(rotateframe.I(), D3DVAL(4), D3DVAL(0), D3DVAL(0));
  87.  
  88.     // Set up the camera frame's position. Objects with the same x-value and
  89.     // y-value as the camera will appear straight ahead.
  90.     // Negative z-values are farther away, making the object look
  91.     // smaller as the negative numbers increase.
  92.     camera->SetPosition(scene->I(), D3DVAL(0), D3DVAL(8), -D3DVAL(16));
  93.     camera->LookAt( rotateframe.I(), scene->I(), D3DRMCONSTRAIN_Z );
  94.  
  95.     // Initialize the lights in the scene, creating a light frame that
  96.     // is a child of the scene.
  97.     lights = new CADirect3DRMFrame( lpD3DRM, scene->I() );
  98.  
  99.     // Position the light frame within the scene.
  100.     lights->SetPosition(scene->I(), D3DVAL(5), D3DVAL(0), -D3DVAL(7));
  101.     lights->SetOrientation( scene->I(),  1, -1, 1,  1, 1, 1);
  102.  
  103.     light1 = new CADirect3DRMLight( lpD3DRM, D3DRMLIGHT_DIRECTIONAL,
  104.                                     D3DRGB( D3DVAL(1.0), D3DVAL(0.8), D3DVAL(0.9)));
  105.     lights->AddLight( light1->I() );
  106.  
  107.     // Create a dim, ambient light and add it to the scene frame,
  108.     // applying it to the whole scene. Ambient light comes from all 
  109.     // directions, so a bright ambient light would wash out the object.
  110.     light2 = new CADirect3DRMLight( lpD3DRM, D3DRMLIGHT_AMBIENT, D3DRGB( 
  111.                                     D3DVAL(.1), D3DVAL(0.1), D3DVAL(0.1)));
  112.     lights->AddLight( light2->I() );
  113.     
  114.     // setup the shadows on the spider
  115.     LPVOID pVoid = NULL;
  116.     lpD3DRM->I()->QueryInterface( IID_IDirect3DRM2, (void **) &pVoid );
  117.     if (pVoid)  //  DX5 or greater
  118.     {
  119.       LPDIRECT3DRMVISUAL pShadow;
  120.       lpD3DRM->CreateShadow( myglobs.spider->I(), light1->I(),
  121.         D3DVAL(0), D3DVAL(-1), D3DVAL(0),
  122.         D3DVAL(0), D3DVAL(1), D3DVAL(0),
  123.         &pShadow);
  124.  
  125.       rotateframe.AddVisual( pShadow );
  126.       pShadow->Release();
  127.     }
  128.     else  // DX3 has limited shadow support
  129.     {
  130.       LPDIRECT3DRMVISUAL pShadow;
  131.       lpD3DRM->CreateShadow( myglobs.spider->m_pMeshBuilder->I(), light1->I(),
  132.         D3DVAL(0), D3DVAL(-1), D3DVAL(0),
  133.         D3DVAL(0), D3DVAL(1), D3DVAL(0),
  134.         &pShadow);
  135.  
  136.       myglobs.spider->AddVisual( pShadow );
  137.       pShadow->Release();
  138.     }
  139.  
  140.     // Clean up.
  141.     delete lights;
  142.     delete light1;
  143.     delete light2;
  144.     return TRUE;
  145. }
  146.  
  147.  
  148.  
  149.  
  150. //-----------------------------------------------------------------------------
  151. // Name: RenderScene()
  152. // Desc: Clear the viewport, render the next frame, and update the window.
  153. //-----------------------------------------------------------------------------
  154. static BOOL RenderScene()
  155. {
  156.     // Move the scene.
  157.     myglobs.scene->Move(D3DVAL(1.0));
  158.     myglobs.spider->Tick();
  159.  
  160.     // Clear the viewport.
  161.     myglobs.view->Clear();
  162.  
  163.     // Render the scene to the viewport.
  164.     myglobs.view->Render(myglobs.scene->I());
  165.  
  166.     // Update the window.
  167.     myglobs.dev->Update();
  168.  
  169.     return TRUE;
  170. }
  171.  
  172.  
  173.  
  174.  
  175. //-----------------------------------------------------------------------------
  176. // Name: AddMediaPath()
  177. // Desc: Looks in the system registry to determine the media path for the
  178. //       sample. Then, it adds that path to the string passed in, checks if the
  179. //       file exists, and returns a path to the file.
  180. //-----------------------------------------------------------------------------
  181. VOID AddMediaPath( CADirect3DRM * pD3DRM )
  182. {
  183.     HKEY   key;
  184.     LONG   result;
  185.     TCHAR  strPath[512];
  186.     DWORD  type, size = 512;
  187.  
  188.     // Open the registry
  189.     result = RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\Microsoft\\DirectX",
  190.                            0, KEY_READ, &key );
  191.     if( ERROR_SUCCESS != result )
  192.         return;
  193.  
  194.     // Search for the desired registry value, and close the registry
  195.     result = RegQueryValueEx( key, "DX6SDK Samples Path", NULL, &type, 
  196.                               (BYTE*)strPath, &size );
  197.     RegCloseKey( key );
  198.  
  199.     if( ERROR_SUCCESS != result )
  200.         return;
  201.  
  202.     strcat( strPath, "\\D3DRM\\Media" );
  203.  
  204.     pD3DRM->AddSearchPath( strPath );
  205.  
  206.     return;
  207. }
  208.  
  209.  
  210.  
  211.  
  212. //-----------------------------------------------------------------------------
  213. // Name: CreateObjects()
  214. // Desc: Initialize globals, create the device and objects. 
  215. //-----------------------------------------------------------------------------
  216. BOOL CreateObjects( HWND win )
  217. {
  218.     RECT rc;      // Bounding rectangle for main window 
  219.     int width;    // Device's width 
  220.     int height;   // Device's height 
  221.  
  222.  
  223.     // Initialize the entire global variable structure to zero. 
  224.     memset(&myglobs, 0, sizeof(myglobs));
  225.  
  226.     // Create a DirectDrawClipper object and associate the window with it.
  227.     lpDDClipper = new CADirectDrawClipper();
  228.     lpDDClipper->SetHWnd(0, win);
  229.  
  230.     // Create the Direct3DRM object.
  231.     lpD3DRM = new CADirect3DRM();
  232.     
  233.     // Create a default Direct3DRM device.
  234.     GetClientRect(win, &rc);
  235.  
  236.     myglobs.dev = new CADirect3DRMDevice( lpD3DRM, lpDDClipper->I(),  
  237.                            rc.right, rc.bottom);
  238.  
  239.     // Create the master scene frame and the camera frame.
  240.     myglobs.scene = new CADirect3DRMFrame( lpD3DRM, NULL );
  241.     myglobs.scene->SetSceneBackgroundRGB( D3DVAL(0), D3DVAL(0), D3DVAL(.4) );
  242.  
  243.     myglobs.camera = new